package com.ccp.dev.workflow.service.bpm;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.consts.SystemConst;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysOrgService;
import com.ccp.dev.workflow.dao.BpmRunLogDao;
import com.ccp.dev.workflow.dao.ProcessRunDao;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.bpm.BpmRunLog;
import com.ccp.dev.workflow.service.ProcessRunService;
import org.springframework.stereotype.Service;


@Service
public class BpmRunLogService extends BaseService<BpmRunLog> {
    @Resource
    private BpmRunLogDao bpmRunLogDao;
    @Resource
    private ProcessRunDao processRunDao;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private ProcessRunService processRunService;

    public BpmRunLogService() {

    }

    /**
     * 通过用户ID获取用户操作的流程日志
     *
     * @param userId 用户ID
     * @return
     */
    public List<BpmRunLog> getByUserId(Long userId) {
        return bpmRunLogDao.getByUserId(userId);
    }

    /**
     * 通过流程运行ID获取流程的操作日志
     *
     * @param runId 流程运行ID
     * @return
     */
    public List<BpmRunLog> getByRunId(String runId) {
        return bpmRunLogDao.getByRunId(runId);
    }

    /**
     * 根据流程运行ID删除流程的操作日志
     *
     * @param runId
     */
    public void delByRunId(String runId) {
        bpmRunLogDao.delByRunId(runId);
    }

    /**
     * 添加流程运行日志
     * @param runId
     * @param operatorType
     * @param memo
     */
    public void addRunLog(String runId, Integer operatorType, String memo) {
        ProcessRun processRun = processRunService.getById(runId);
        this.addRunLog(processRun, operatorType, memo);
    }

    /**
     * 添加流程运行日志。
     *
     * @param user
     * @param runId
     * @param operatorType
     * @param memo
     */
    public void addRunLog(SysUser user, String runId, Integer operatorType, String memo) {
        ProcessRun processRun = processRunService.getById(runId);
        this.addRunLog(user, processRun, operatorType, memo);
    }

    /**
     * 添加流程运行日志
     * @param processRun
     * @param operatorType
     * @param memo
     */
    public void addRunLog(ProcessRun processRun, Integer operatorType, String memo) {

        SysUser user = (SysUser) ContextUtil.getCurrentUser();
        String userId = SystemConst.SYSTEMUSERID;
        String userName = SystemConst.SYSTEMUSERNAME;
        if (user != null) {
            userId = user.getUserId();
            userName = user.getFullName();
        }
        Date now = Calendar.getInstance().getTime();
        BpmRunLog runLog = new BpmRunLog();
        runLog.setId(UUIDUtils.getUUIDFor32());
        runLog.setUserId(userId);
        runLog.setUserName(userName);
        if (processRun != null) {
            runLog.setRunId(processRun.getRunId());
            runLog.setProcessSubject(processRun.getSubject());
        }
        runLog.setCreateTime(now);
        runLog.setOperatorType(operatorType);
        runLog.setMemo(memo);
        String tenId = ContextUtil.getCurrentUserTenantId();
        runLog.setTenantId(tenId);

        bpmRunLogDao.add(runLog);
    }

    /**
     * 添加流程运行日志。
     *
     * @param user
     * @param processRun
     * @param operatortype
     * @param memo
     */
    public void addRunLog(SysUser user, ProcessRun processRun, Integer operatortype, String memo) {

        boolean isAdmin = ContextUtil.isSuperAdmin();
        BpmRunLog runLog = new BpmRunLog();
        runLog.setId(UUIDUtils.getUUIDFor32());
        runLog.setUserId(user.getUserId());
        runLog.setUserName(user.getFullName());
        runLog.setRunId(processRun.getRunId());
        runLog.setProcessSubject(processRun.getSubject());
        runLog.setCreateTime(new Date());
        runLog.setOperatorType(operatortype);
        runLog.setMemo(memo);
        if (!isAdmin) {
            String tenId = ContextUtil.getCurrentUserTenantId();
            runLog.setTenantId(tenId);
        }
        bpmRunLogDao.add(runLog);
    }

    /**
     * 流程操作日志列表
     * @param filter
     * @return
     */

    public List<BpmRunLog> getAll(QueryFilter filter) {
        return bpmRunLogDao.getAll(filter);
    }

    /**
     * 流程日志删除
     * @param lAryId
     */
    public void delByIds(String[] lAryId) {
        for (String s :lAryId){
            bpmRunLogDao.delById(s);
        }
    }

    public BpmRunLog getById(String id) {
        return  bpmRunLogDao.getById(id);
    }
}
